<%--
  Created by IntelliJ IDEA.
  User: teacher
  Date: 2022/4/18
  Time: 10:56
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <meta charset="UTF-8">
    <title>我的购物车</title>
    <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-3.4.1.min.js"></script>
    <script>
        $(function(){
           // 全选操作
            $("#all").click(function(){
               $("input[type='checkbox']").prop("checked",$(this).prop("checked"));
            });
            // 修改购物车明细数量
            $("a[name='modifyCart']").click(function(){
                let id = $(this).attr("data-id");
                // $(参数一,参数二):表示在参数二的范围中查找符合参数一对应的选择器规则的所有元素
                // 参数一:选择器规则
                // 参数二:查找范围,值表示的是一个元素的范围
                let num = $("input[name='num']",$(this).parent().parent()).val();
                console.log("id:"+id+",num:"+num);
                location.href = "${pageContext.request.contextPath}/modify.cart?id="+id+"&num="+num;
            });
            // 删除选中项
            $("#removeByIds").click(function(){
               $("#cartForm").submit();
            });
        });
    </script>
</head>
<body>
<h1>我的购物车</h1>
<hr/>
<form id="cartForm" action="${pageContext.request.contextPath}/removeByIds.cart" method="post">
<table border="1">
    <tr>
        <th>
            <input type="checkbox" id="all">全选
        </th>
        <th>序号</th>
        <th>商品名称</th>
        <th>商品数量</th>
        <th>商品总价</th>
        <th>操作</th>
    </tr>
    <c:forEach items="${cart.items}" var="item" varStatus="i">
        <tr>
            <td>
                <input type="checkbox" name="productId" value="${item.product.id}"/>
            </td>
            <td>${i.index+1}</td>
            <td>${item.product.name}</td>
            <td>
                <input type="text" name="num" value="${item.num}"/>
            </td>
            <td>${item.price}</td>
            <td>
                <a href="javascript:;" name="modifyCart" data-id="${item.product.id}">修改</a>
                <a href="${pageContext.request.contextPath}/removeById.cart?id=${item.product.id}" onclick="return confirm('是否确认删除')">删除</a>
            </td>
        </tr>
    </c:forEach>
</table>
</form>
--------------------------------------------------------<br/>
总计:${cart.price}元<br/>
<a href="javascript:;" id="removeByIds">删除选中项</a>
<a href="${pageContext.request.contextPath}/clean.cart">清空购物车</a>
<a href="${pageContext.request.contextPath}/findAll.product">继续购物</a>
<a href="${pageContext.request.contextPath}/jsp/confirm.jsp">结算</a><br/>
<span style="color:red;">${cartMsg}</span>
</body>
</html>